﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Transport
{
    public class GiamGiaControl
    {
        transportEntities db;

        public GiamGiaControl()
        {
            db = new transportEntities();
        }

        public List<giam_gia> listAll()
        {
            try
            {
                return db.giam_gia.ToList();
            }
            catch (Exception)
            {
                return null;
            }
        }

        public double findByUserName(string Id)
        {
            try
            {
                giam_gia gg = db.giam_gia.SingleOrDefault(t => t.ten_dang_nhap == Id);
                if (gg != null)
                    return gg.ti_le_giam_gia;
                else
                    return 0;
            }
            catch (Exception)
            {
                return 0;
            }
        }

        public bool insert(giam_gia value)
        {
            try
            {
                db.giam_gia.AddObject(value);
                db.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool update(giam_gia value)
        {
            try
            {
                giam_gia st = db.giam_gia.SingleOrDefault(t => t.ten_dang_nhap == value.ten_dang_nhap);
                st = value;
                db.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool delete(string Id)
        {
            try
            {
                giam_gia st = db.giam_gia.SingleOrDefault(t => t.ten_dang_nhap == Id);
                db.giam_gia.DeleteObject(st);
                db.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}